package sunyu.controller.common.hbase;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sunyu.service.hbase.HbaseService;
import sunyu.tools.hbase.pojo.Row;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/api/hbase")
public class ApiHbaseController {

    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());
    private static final String canTable = "bc_msg";//CAN数据的表名
    private static final String canFamily = "msg";//默认CAN列族
    @Resource(name = "hbaseService")
    private HbaseService hbaseService;

    @RequestMapping("/findCan")
    @ResponseBody
    public List<Row> findCan(@RequestParam String did,
                             @RequestParam(required = false) String startTime,
                             @RequestParam(required = false) String endTime,
                             @RequestParam(required = false) Boolean reversed,
                             @RequestParam(required = false) Integer pageSize,
                             @RequestParam(required = false) Boolean isFirstPage,
                             @RequestParam(required = false) String startRowName,
                             @RequestParam(required = false) String columns) {
        logger.debug("DID=>" + did);
        logger.debug("startTime=>" + startTime);
        logger.debug("endTime=>" + endTime);
        logger.debug("reversed=>" + reversed);
        logger.debug("pageSize=>" + pageSize);
        logger.debug("isFirstPage=>" + isFirstPage);
        logger.debug("startRowName=>" + startRowName);
        logger.debug("columns=>" + columns);
        List<String> columnList = null;
        if (columns != null) {
            columnList = new ArrayList<>();
            for (String column : columns.split(",")) {
                columnList.add(canFamily + ":" + column);
            }
        }
        logger.debug("columns=>" + JSON.toJSONString(columnList));
        return hbaseService.find(canTable, did, startTime, endTime, reversed, null, pageSize, isFirstPage, startRowName, Arrays.asList(canFamily), columnList);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed,
                             @PathVariable("did") String did,
                             @PathVariable("pageSize") Integer pageSize) {
        return hbaseService.find(canTable, did, null, null, reversed, null, pageSize, true, null, null, null);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}/{startTime}/{endTime}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed,
                             @PathVariable("did") String did,
                             @PathVariable("pageSize") Integer pageSize,
                             @PathVariable("startTime") String startTime,
                             @PathVariable("endTime") String endTime) {
        return hbaseService.find(canTable, did, startTime, endTime, reversed, null, pageSize, true, null, null, null);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}/{startRowName}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed,
                             @PathVariable("did") String did,
                             @PathVariable("pageSize") Integer pageSize,
                             @PathVariable("startRowName") String startRowName) {
        return hbaseService.find(canTable, did, null, null, false, null, pageSize, false, startRowName, null, null);
    }

    @RequestMapping(value = "/findCan/{reversed}/{did}/{pageSize}/{startRowName}/{startTime}/{endTime}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Row> findCan(@PathVariable("reversed") Boolean reversed,
                             @PathVariable("did") String did,
                             @PathVariable("pageSize") Integer pageSize,
                             @PathVariable("startRowName") String startRowName,
                             @PathVariable("startTime") String startTime,
                             @PathVariable("endTime") String endTime) {
        return hbaseService.find(canTable, did, startTime, endTime, false, null, pageSize, false, startRowName, null, null);
    }

    @RequestMapping(value = "/countCan/{did}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Long countCan(@PathVariable("did") String did) {
        return hbaseService.count(canTable, did, null, null, null);
    }

    @RequestMapping(value = "/countCan/{did}/{startTime}/{endTime}", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Long countCan(@PathVariable("did") String did,
                         @PathVariable("startTime") String startTime,
                         @PathVariable("endTime") String endTime) {
        return hbaseService.count(canTable, did, startTime, endTime, null);
    }

}
